﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.DAL.RoleDTO;
using MesBSW.Tools;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.Dialog.Role
{
    public partial class UserInfoDia : UIEditForm
    {
        #region "内部变量"
        private BaseUserDTO curr_user = null;
        private UserInfoControler ctrl = new UserInfoControler();
        private List<BaseDeptModel> deptList = new List<BaseDeptModel>();
        #endregion


        public UserInfoDia()
        {
            InitializeComponent();
            this.SetDeptComboboxBinding();
            this.SetControlValueForUser(null);
            this.deptList = this.ctrl.SearchAllDept();
        }

        public UserInfoDia(BaseUserDTO user)
        {
            InitializeComponent();
            this.SetDeptComboboxBinding();

            this.curr_user = user;
            this.SetControlValueForUser(user);
            this.deptList = this.ctrl.SearchAllDept();
        }


        #region "控件事件"
        private void rbnSexM_Click(object sender, EventArgs e)
        {
            this.rbnSexM.Checked = true;
            this.rbnSexW.Checked = false;
        }

        private void rbnSexW_Click(object sender, EventArgs e)
        {
            this.rbnSexW.Checked = true;
            this.rbnSexM.Checked = false;
        }


        #endregion

        #region "内部方法"
        /// <summary>
        /// 界面Load时，将所有部门的信息读取到部门下拉列表中
        /// </summary>
        private  void SetDeptComboboxBinding()
        {
            this.cmbDept.Nodes.Clear();
            List<TreeNode> tmpList = this.ctrl.SearchAllDeptIntoNode();
            if (tmpList != null && tmpList.Count > 0)
            {
                this.cmbDept.Nodes.AddRange(tmpList.ToArray());
            }
        }

        /// <summary>
        /// 将用户信息放到界面中相应的控件中
        /// </summary>
        /// <param name="user"></param>
        private void SetControlValueForUser(BaseUserDTO user)
        {
            if (user != null)
            {
                this.txtUserName.Text = user.user_name;
                this.txtUserName.Tag = user.id;
                this.txtUserName.Enabled = false;

                this.txtRealName.Text = user.real_name;
                this.chkIsDel.Checked = user.is_del ?? false;
                if (user.sex ?? false)
                {
                    this.rbnSexM.Checked = true;
                    this.rbnSexW.Checked = false;
                }
                else
                {
                    this.rbnSexM.Checked = false;
                    this.rbnSexW.Checked = true;
                }
                this.txtPwd.Text = user.pwd;
                this.txtPhone.Text = user.phone;
                this.txtEmail.Text = user.email;
                this.txtRemark.Text = user.des;
                if (user.deptModel != null)
                {
                    this.cmbDept.Text = user.deptModel.dept_name;
                }
            }
            else
            {
                txtUserName.Text = "";
                txtUserName.Tag = -999;
                txtUserName.Enabled = true;

                txtRealName.Text = "";
                txtRealName.Enabled = true;

                this.chkIsDel.Checked = false;
                this.rbnSexM.Checked = true;
                this.rbnSexW.Checked = false;
                this.txtPwd.Text = "";
                this.txtPhone.Text = "";
                this.txtEmail.Text = "";
                this.txtRemark.Text = "";
            }
        }

        /// <summary>
        /// 取出界面中各控件的值，组成个user类
        /// </summary>
        /// <returns></returns>
        private BaseUserDTO InitBaseUser()
        {
            BaseUserDTO resBean = new BaseUserDTO();
            resBean.id = Convert.ToInt32(this.txtUserName.Tag);
            resBean.user_name = this.txtUserName.Text;
            resBean.real_name = this.txtRealName.Text;
            resBean.is_del = this.chkIsDel.Checked;
            resBean.pwd = this.txtPwd.Text;
            resBean.sex = this.rbnSexM.Checked == true ? true : false;
            resBean.phone = this.txtPhone.Text;
            resBean.email = this.txtEmail.Text;
            resBean.des = this.txtRemark.Text;
            var query = this.deptList.Where(d => d.dept_name.Equals(this.cmbDept.Text));
            if (query != null && query.Count() > 0)
            {
                resBean.base_dept_id = query.First().id;
            }
            return resBean;
        }

        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            BaseUserDTO tmpUser = this.InitBaseUser();
            try
            {
                BaseUserModel user = new BaseUserModel();
                EntityTools.CopyEntity(tmpUser, ref user);
                if (tmpUser.id > 0)
                {
                    if (EntityTools.IsDiffForValue(this.curr_user, tmpUser) == false)
                    {
                        if (this.ctrl.UpdateUser(user))
                        {
                            UIMessageBox.ShowSuccess("用户信息已经成功保存！");
                        }
                        else
                        {
                            UIMessageBox.ShowWarning("用户信息保存失败！");
                        }
                    }
                }
                else
                {
                    if (this.ctrl.InsertUser(user))
                    {
                        UIMessageBox.ShowSuccess("用户信息已经成功保存！");
                    }
                    else
                    {
                        UIMessageBox.ShowWarning("用户信息保存失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowError("用户信息保存失败！" + "\r\n" + ex.Message);
            }
        }
    }
}
